package com.xiaojiang.blog.util;


import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * @Auther: JiangShiyu
 * @Date: 2023/3/12 20:38
 * @Description: TODO 上传文件工具类
 */
public class BlogUploadFileUtils {
    /**
    * @description: 获取图片后缀
     * @param file :
    * @return:  java.lang.String
    * @author xiaojiang
    * @date: 2023/3/12 20:46
    **/
    public static String getSuffixName(MultipartFile file){
        String fileName = file.getOriginalFilename();
        if (StringUtils.hasText(fileName)){
            throw new RuntimeException("获取图片后缀失败");
        }
        return fileName.substring(fileName.lastIndexOf("."));
    }
    /**
     * @Description: 生成文件名称通用方法
     * @Param: [suffixName] 图片后缀
     * @return: java.lang.String
     * @author xiaojiang
     * @date: 2023/3/12 20:49
     */
    public static String getNewFileName(String suffixName){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
        Random r = new Random();
        StringBuilder tempName = new StringBuilder();
        tempName.append(sdf.format(new Date())).append(r.nextInt(100)).append(suffixName);
        return tempName.toString();
    }
}
